package com.dnjn.mes.biz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.mes.biz.assist.ext.Wrapperz;
import com.dnjn.mes.biz.domain.*;
import com.dnjn.mes.biz.enums.BoolType;
import com.dnjn.mes.biz.service.IMesEquipmentService;
import com.dnjn.mes.biz.service.IMesRecordCapacityService;
import com.dnjn.mes.biz.service.IMesWorkforceConfigService;
import com.dnjn.mes.biz.service.IMesWorkforceShiftService;
import com.dnjn.mes.common.core.controller.BaseController;
import com.dnjn.mes.common.core.domain.AjaxResult;
import com.dnjn.mes.common.core.domain.R;
import com.dnjn.mes.common.utils.StringUtils;
import com.dnjn.mes.common.utils.poi.ExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * <p>
 * 产能表 前端控制器
 * </p>
 *
 * @author Amber
 * @since 2023-07-26
 */
@RestController
@RequestMapping("/biz/mesRecordCapacity")
public class MesRecordCapacityController extends BaseController {

    @Autowired
    protected IMesRecordCapacityService baseService;

    @Autowired
    protected IMesEquipmentService equipmentService;

    @Autowired
    protected IMesWorkforceConfigService mesWorkforceConfigService;

    @Autowired
    private IMesWorkforceShiftService shiftService;

    @GetMapping("/get/{id}")
    public AjaxResult get(@PathVariable Long id) {
        return success(baseService.getById(id));
    }

    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody MesRecordCapacity domain) {
        domain.setCreateBy(getUsername());
        return toAjax(baseService.save(domain));
    }

    @PutMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody MesRecordCapacity domain) {
        return toAjax(baseService.updateById(domain));
    }

    @DeleteMapping("/{pkIds}")
    public AjaxResult remove(@PathVariable Long[] pkIds) {
        baseService.removeBatchByIds(Arrays.asList(pkIds));
        return success();
    }

    @PostMapping("/page")
    public AjaxResult page(@RequestBody MesRecordCapacity domain, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Page page = Page.of(current, size);
        Page<MesRecordCapacity> list = baseService.page(page, Wrappers.query(domain));
        return success(list);
    }

    @PostMapping("/export")
    public void export(HttpServletResponse response, MesRecordCapacity domain) {
        List<MesRecordCapacity> list = baseService.list(Wrappers.query(domain));
        ExcelUtil<MesRecordCapacity> util = new ExcelUtil<MesRecordCapacity>(MesRecordCapacity.class);
        util.exportExcel(response, list, "参数数据");
    }

    /**
     * 产量统计->统计每天总产量
     * startTime: 2023-08-22
     * endTime: 2023-09-01
     */
    @Operation(summary = "统计每天总产量",tags = {"数据统计"})
    @PostMapping("/countRecordCapacitys")
    public AjaxResult countRecordCapacitys(@RequestParam(required = false) @Parameter(description = "yyyy-MM-dd") String startTime, @RequestParam(required = false) @Parameter(description = "yyyy-MM-dd") String endTime,
                                           @RequestHeader(value = "equipmentId") long equipmentId) throws ParseException {
        //如果两个日期为空时，默认获取当前日期前一个月的
        if (StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)) {
            LocalDateTime endLocalDateTime = LocalDateTime.now();
            LocalDateTime startLocalDateTime = LocalDateTime.now().minusMonths(1);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            startTime = formatter.format(startLocalDateTime);
            endTime = formatter.format(endLocalDateTime);
        }
        List<MesRecordCapacity> list = baseService.countRecordCapacitys(startTime, endTime, equipmentId);
        return success(list);
    }


    /**
     * 产量统计->每个班次每天产量
     * startTime: 2023082202
     * endTime: 2023090103
     */
    @Operation(summary = "每个班次每天产量",tags = {"数据统计"})
    @PostMapping("/countWorkforceRecordCapacitys")
    public AjaxResult countWorkforceRecordCapacitys(@RequestParam(required = false) @Parameter(description = "yyyy-MM-dd") String startTime, @RequestParam(required = false) @Parameter(description = "yyyy-MM-dd") String endTime,
                                           @RequestHeader(value = "equipmentId") long equipmentId) throws ParseException {
        //如果两个日期为空时，默认获取当前日期前一个月的
        if (StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)) {
            LocalDateTime endLocalDateTime = LocalDateTime.now();
            LocalDateTime startLocalDateTime = LocalDateTime.now().minusMonths(1);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            startTime = formatter.format(startLocalDateTime);
            endTime = formatter.format(endLocalDateTime);
        }
        QueryWrapper<MesWorkforceConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted","0");
        List<MesWorkforceConfig> workforceConfigList = mesWorkforceConfigService.list(queryWrapper);
        if (null == workforceConfigList || workforceConfigList.size() == 0){
            return error("班次列表为空");
        }
        List<List<MesWorkforceShift>> listList = new ArrayList<>();
        for (MesWorkforceConfig mesWorkforceConfig : workforceConfigList) {
            List<MesWorkforceShift> list = shiftService.countWorkforceRecordCapacitys(startTime, endTime, equipmentId,mesWorkforceConfig.getWorkforceName());
            listList.add(list);
        }

        return success(listList);
    }
}